import { t } from "@/locales";
import { Avatar, FormRule, PrimaryTableCol, SelectProps, Space, Tag } from "tdesign-vue-next";
import { SearchParamsProps } from "@/components/s-table/type";
import { fileView } from "@/api/upload";
import SImageView from '@/components/ImageView/index.vue';
/**
 * 搜索表单项
 */
export const searchParams: SearchParamsProps[] = [
    {
        label: t('pages.sysUser.username'),
        key: 'username',
    },
    {
        label: t('pages.sysUser.nickname'),
        key: 'nickname',
    },
];

/**
 * 表格列
 */
export const COLUMNS: PrimaryTableCol[] = [
    {
        title: t('pages.sysUser.username'),
        ellipsis: true,
        align: 'center',
        colKey: 'username',
    },
    {
        title: t('pages.sysUser.sex'),
        ellipsis: true,
        align: 'center',
        colKey: 'sex',
        cell:(h, {row}) => h(Tag,
          {theme:'danger',
           variant:'light',
        
          },row.sex == 0? '男 ' :row.sex == 1? '女' : '未知')
    },
    {
        title: t('pages.sysUser.nickname'),
        ellipsis: true,
        align: 'center',
        colKey: 'nickname',
    },
    {
        title: t('pages.sysUser.phone'),
        ellipsis: true,
        align: 'center',
        colKey: 'phone',
    },
    {
        title: t('pages.sysUser.email'),
        ellipsis: true,
        align: 'center',
        colKey: 'email',
    },
    {
        title: t('pages.sysUser.role'),
        ellipsis: true,
        align: 'center',
        cell: (h, { row }) => h(Space,{direction:'vertical'}, row.roles.map((i:any) => h(Tag,{key:i.value},i.label)))
        
    },
    {
        title: t('pages.sysUser.avatar'),
        ellipsis: true,
        width: 90,
        align: 'center',
        colKey: 'avatar',
        cell: (h, { row }) => {
                    if(row.avatar){
                        try{
                            return h(SImageView, { images: [fileView(row.avatar)]});
                        }catch(e){
                            return h('div', '-');
                        }
                    }
                    return '-';
                }
        
    },
    {
        title: t('pages.sysUser.remarks'),
        ellipsis: true,
        align: 'center',
        colKey: 'remarks',
    },
];

/**
 * 表单校验规则
 */
export const FORM_RULES: Record<string, FormRule[]> = {
    username: [{required: true, message: t('pages.sysUser.required.username'), type: 'error', trigger: 'blur'}],
    password: [{ required: true, message: t('pages.sysUser.required.password'), type: 'error', trigger: 'blur' }],
    email: [{ email: true, message: t('pages.sysUser.required.email'), type: 'error', trigger: 'blur' }],
}

export const sexOptions: SelectProps['options'] = [
  {
      label: t('components.sex.Male'),
      value: 0
  },
  {
      label: t('components.sex.Female'),
      value: 1
  },
  {
      label: t('components.sex.Unknown'),
      value: 2
  }

]
